const vscode = require("vscode");
const os = require("os");
const path = require("path");
const fs = require("fs");
let _globalAppRoot = null;
function setAppRoot(_0x50115f) {
  _globalAppRoot = _0x50115f;
  console.log("[pathUtils] 设置 appRoot:", _0x50115f);
}
function getAppRoot() {
  return _globalAppRoot;
}
function findCursorDatabase(_0x5274f6) {
  const _0x5c4f2e = os.homedir();
  const _0x3d5688 = process.env.SUDO_USER;
  const _0x2f7b0e = [];
  _0x2f7b0e.push(path.join(_0x5c4f2e, ".config", _0x5274f6.toLowerCase(), "User", "globalStorage", "state.vscdb"), path.join(_0x5c4f2e, ".config", _0x5274f6, "User", "globalStorage", "state.vscdb"));
  if (_0x3d5688 && _0x3d5688 !== "root") {
    _0x2f7b0e.push(path.join("/home", _0x3d5688, ".config", _0x5274f6.toLowerCase(), "User", "globalStorage", "state.vscdb"), path.join("/home", _0x3d5688, ".config", _0x5274f6, "User", "globalStorage", "state.vscdb"));
  }
  if (_0x5c4f2e === "/root") {
    _0x2f7b0e.push(path.join("/root", ".config", _0x5274f6.toLowerCase(), "User", "globalStorage", "state.vscdb"), path.join("/root", ".config", _0x5274f6, "User", "globalStorage", "state.vscdb"));
  }
  _0x2f7b0e.push(path.join(_0x5c4f2e, ".local", "share", _0x5274f6.toLowerCase(), "User", "globalStorage", "state.vscdb"));
  for (const _0xc57193 of _0x2f7b0e) {
    if (fs.existsSync(_0xc57193)) {
      return _0xc57193;
    }
  }
  return null;
}
function getKeyFiles(_0x301e2a = "Cursor") {
  const _0x52043f = process.platform;
  const _0x45cbca = vscode.env.appRoot;
  let _0x3e6c23 = os.homedir();
  if (_0x52043f === "linux" && _0x3e6c23 === "/root") {
    const _0x5b8613 = process.env.SUDO_USER;
    if (_0x5b8613 && _0x5b8613 !== "root") {
      _0x3e6c23 = path.join("/home", _0x5b8613);
    }
  }
  let _0x3bb770 = path.join(_0x45cbca, "resources", "app");
  if (!fs.existsSync(_0x3bb770)) {
    _0x3bb770 = _0x45cbca;
  }
  const _0x4c48f7 = {
    distMain: path.join(_0x3bb770, "extensions", "cursor-always-local", "dist", "main.js"),
    outMain: path.join(_0x3bb770, "out", "main.js"),
    workbench: path.join(_0x3bb770, "out", "vs", "workbench", "workbench.desktop.main.js"),
    extHost: path.join(_0x3bb770, "out", "vs", "workbench", "api", "node", "extensionHostProcess.js")
  };
  let _0x4f373b;
  const _0x2e4e1f = [];
  switch (_0x52043f) {
    case "win32":
      _0x4f373b = path.join(_0x3e6c23, "AppData", "Roaming", _0x301e2a);
      break;
    case "darwin":
      _0x4f373b = path.join(_0x3e6c23, "Library", "Application Support", _0x301e2a);
      break;
    case "linux":
      _0x4f373b = path.join(_0x3e6c23, ".config", _0x301e2a.toLowerCase());
      _0x2e4e1f.push(path.join(_0x3e6c23, ".config", _0x301e2a.toLowerCase()), path.join(_0x3e6c23, ".config", _0x301e2a), path.join(_0x3e6c23, ".local", "share", _0x301e2a.toLowerCase()), process.env.SUDO_USER ? path.join("/root", ".config", _0x301e2a.toLowerCase()) : null);
      break;
    default:
      _0x4f373b = path.join(_0x3e6c23, ".config", _0x301e2a.toLowerCase());
  }
  let _0x20c9c3;
  if (_0x52043f === "linux") {
    _0x20c9c3 = findCursorDatabase(_0x301e2a);
    if (!_0x20c9c3) {
      _0x20c9c3 = path.join(_0x4f373b, "User", "globalStorage", "state.vscdb");
    }
  } else {
    _0x20c9c3 = path.join(_0x4f373b, "User", "globalStorage", "state.vscdb");
  }
  _0x4c48f7.stateDb = _0x20c9c3;
  _0x4c48f7.cursorInfo = path.join(_0x3e6c23, ".cursor_info");
  let _0x5afc54;
  switch (_0x52043f) {
    case "win32":
      _0x5afc54 = path.join(_0x3e6c23, "AppData", "Roaming", "CursorPro");
      break;
    case "darwin":
      _0x5afc54 = path.join(_0x3e6c23, "Library", "Application Support", "CursorPro");
      break;
    case "linux":
      _0x5afc54 = path.join(_0x3e6c23, ".config", "CursorPro");
      break;
    default:
      _0x5afc54 = path.join(_0x3e6c23, ".config", "CursorPro");
      break;
  }
  _0x4c48f7.configJson = path.join(_0x5afc54, "config.json");
  const _0x4e7763 = {};
  for (const [_0x3f1794, _0x578dbc] of Object.entries(_0x4c48f7)) {
    if (fs.existsSync(_0x578dbc)) {
      _0x4e7763[_0x3f1794] = _0x578dbc;
    } else {
      _0x4e7763[_0x3f1794] = undefined;
    }
  }
  return _0x4e7763;
}
function checkRequiredFiles(_0xbff127) {
  const _0x294fa0 = {
    hasStateDb: !!_0xbff127.stateDb,
    hasAnyTargetFile: !!_0xbff127.distMain || !!_0xbff127.outMain || !!_0xbff127.workbench || !!_0xbff127.extHost,
    missingFiles: [],
    availableFiles: []
  };
  const _0xb3c477 = _0x294fa0;
  for (const [_0x17f5b7, _0xcaf39c] of Object.entries(_0xbff127)) {
    if (_0xcaf39c) {
      _0xb3c477.availableFiles.push(_0x17f5b7);
    } else {
      _0xb3c477.missingFiles.push(_0x17f5b7);
    }
  }
  return _0xb3c477;
}
function getCursorUserDataPath() {
  const _0x8cfc88 = process.platform;
  const _0x40cc48 = os.homedir();
  switch (_0x8cfc88) {
    case "win32":
      return path.join(_0x40cc48, "AppData", "Roaming", "Cursor");
    case "darwin":
      return path.join(_0x40cc48, "Library", "Application Support", "Cursor");
    case "linux":
      return path.join(_0x40cc48, ".config", "cursor");
    default:
      return path.join(_0x40cc48, ".config", "cursor");
  }
}
function getCursorInstallPath() {
  try {
    const _0x57c0ba = vscode.env.appRoot;
    if (_0x57c0ba && _0x57c0ba.includes("Cursor") || _0x57c0ba.includes("cursor")) {
      return _0x57c0ba;
    }
    return null;
  } catch (_0xffcc2a) {
    return null;
  }
}
function getCursorSettingsPath(_0x4dda1f = "Cursor") {
  const _0x4c734a = process.platform;
  let _0x125e89 = os.homedir();
  if (_0x4c734a === "linux" && _0x125e89 === "/root") {
    const _0x1c4b1d = process.env.SUDO_USER;
    if (_0x1c4b1d && _0x1c4b1d !== "root") {
      _0x125e89 = path.join("/home", _0x1c4b1d);
    }
  }
  switch (_0x4c734a) {
    case "win32":
      return path.join(_0x125e89, "AppData", "Roaming", _0x4dda1f, "User", "settings.json");
    case "darwin":
      return path.join(_0x125e89, "Library", "Application Support", _0x4dda1f, "User", "settings.json");
    case "linux":
      const _0x10e399 = path.join(_0x125e89, ".config", _0x4dda1f, "User", "settings.json");
      const _0x4af27b = path.join(_0x125e89, ".config", _0x4dda1f.toLowerCase(), "User", "settings.json");
      if (fs.existsSync(_0x10e399)) {
        return _0x10e399;
      }
      if (fs.existsSync(_0x4af27b)) {
        return _0x4af27b;
      }
      return _0x10e399;
    default:
      return path.join(_0x125e89, ".config", _0x4dda1f, "User", "settings.json");
  }
}
function cursorSettingsExists(_0x35b38e = "Cursor") {
  const _0x187113 = getCursorSettingsPath(_0x35b38e);
  return fs.existsSync(_0x187113);
}
function readCursorSettings(_0x1e9ef3 = "Cursor") {
  try {
    const _0x43a090 = getCursorSettingsPath(_0x1e9ef3);
    if (!fs.existsSync(_0x43a090)) {
      return null;
    }
    try {
      fs.accessSync(_0x43a090, fs.constants.R_OK);
    } catch (_0x5ad23e) {
      return null;
    }
    const _0x31d03d = fs.readFileSync(_0x43a090, "utf-8");
    const _0x52ac29 = removeComments(_0x31d03d);
    const _0x10b717 = JSON.parse(_0x52ac29);
    return _0x10b717;
  } catch (_0x3fb4b5) {
    return null;
  }
}
function removeComments(_0x1ac494) {
  let _0x18a23e = "";
  let _0x22da7b = false;
  let _0x4ba229 = false;
  let _0x56e68d = false;
  let _0x37eaac = "";
  let _0x5b4ecc = false;
  for (let _0x47763c = 0; _0x47763c < _0x1ac494.length; _0x47763c++) {
    const _0x12b680 = _0x1ac494[_0x47763c];
    const _0x5288c5 = _0x1ac494[_0x47763c + 1];
    if (_0x5b4ecc) {
      if (_0x22da7b) {
        _0x18a23e += _0x12b680;
      }
      _0x5b4ecc = false;
      continue;
    }
    if (_0x12b680 === "\\") {
      if (_0x22da7b) {
        _0x18a23e += _0x12b680;
      }
      _0x5b4ecc = true;
      continue;
    }
    if (_0x22da7b) {
      _0x18a23e += _0x12b680;
      if (_0x12b680 === _0x37eaac) {
        _0x22da7b = false;
        _0x37eaac = "";
      }
      continue;
    }
    if (_0x56e68d) {
      if (_0x12b680 === "*" && _0x5288c5 === "/") {
        _0x56e68d = false;
        _0x47763c++;
      }
      continue;
    }
    if (_0x4ba229) {
      if (_0x12b680 === "\n" || _0x12b680 === "\r") {
        _0x4ba229 = false;
        _0x18a23e += _0x12b680;
      }
      continue;
    }
    if (_0x12b680 === "/" && _0x5288c5 === "/") {
      _0x4ba229 = true;
      _0x47763c++;
      continue;
    }
    if (_0x12b680 === "/" && _0x5288c5 === "*") {
      _0x56e68d = true;
      _0x47763c++;
      continue;
    }
    if (_0x12b680 === "\"" || _0x12b680 === "'") {
      _0x22da7b = true;
      _0x37eaac = _0x12b680;
      _0x18a23e += _0x12b680;
      continue;
    }
    _0x18a23e += _0x12b680;
  }
  return _0x18a23e;
}
function writeCursorSettings(_0x51563e, _0x37bf69 = "Cursor") {
  try {
    const _0xb009a7 = getCursorSettingsPath(_0x37bf69);
    const _0x3daf41 = path.dirname(_0xb009a7);
    if (!fs.existsSync(_0x3daf41)) {
      try {
        fs.mkdirSync(_0x3daf41, {
          recursive: true,
          mode: 493
        });
      } catch (_0x27c063) {
        return false;
      }
    }
    if (fs.existsSync(_0xb009a7)) {
      try {
        fs.accessSync(_0xb009a7, fs.constants.W_OK);
      } catch (_0xf3ffd1) {
        return false;
      }
    }
    const _0x232194 = JSON.stringify(_0x51563e, null, 4);
    fs.writeFileSync(_0xb009a7, _0x232194, {
      encoding: "utf-8",
      mode: 420
    });
    return true;
  } catch (_0x38866c) {
    return false;
  }
}
function updateCursorSettingsField(_0x13104a, _0x3ba788, _0x46e90e = "Cursor") {
  try {
    let _0x503e6d = readCursorSettings(_0x46e90e) || {};
    const _0x656ff0 = _0x13104a.split(".");
    if (_0x656ff0.length === 1) {
      _0x503e6d[_0x13104a] = _0x3ba788;
    } else {
      let _0x4cd25d = _0x503e6d;
      for (let _0x3d64aa = 0; _0x3d64aa < _0x656ff0.length - 1; _0x3d64aa++) {
        if (!_0x4cd25d[_0x656ff0[_0x3d64aa]] || typeof _0x4cd25d[_0x656ff0[_0x3d64aa]] !== "object") {
          _0x4cd25d[_0x656ff0[_0x3d64aa]] = {};
        }
        _0x4cd25d = _0x4cd25d[_0x656ff0[_0x3d64aa]];
      }
      _0x4cd25d[_0x656ff0[_0x656ff0.length - 1]] = _0x3ba788;
    }
    return writeCursorSettings(_0x503e6d, _0x46e90e);
  } catch (_0x165061) {
    return false;
  }
}
function getCursorInfoPath() {
  const _0x263eca = process.platform;
  const _0x1aee70 = os.homedir();
  if (_0x263eca === "darwin" && _globalAppRoot) {
    let _0x453dda = path.join(_globalAppRoot, "resources", "app");
    if (!fs.existsSync(_0x453dda)) {
      _0x453dda = _globalAppRoot;
    }
    const _0x1a24da = path.join(_0x453dda, "extensions", "cursor-always-local", "dist", ".cursor_info");
    console.log("[pathUtils] macOS .cursor_info路径:", _0x1a24da);
    return _0x1a24da;
  } else {
    return path.join(_0x1aee70, ".cursor_info");
  }
}
function cursorInfoExists() {
  const _0x1d4764 = getCursorInfoPath();
  return fs.existsSync(_0x1d4764);
}
function createCursorInfoFile(_0x54b13d = "") {
  try {
    const _0x28b78d = getCursorInfoPath();
    fs.writeFileSync(_0x28b78d, _0x54b13d, "utf-8");
    return true;
  } catch (_0x20fb53) {
    return false;
  }
}
function readCursorInfoFile() {
  try {
    const _0xc68831 = getCursorInfoPath();
    if (!fs.existsSync(_0xc68831)) {
      return null;
    }
    return fs.readFileSync(_0xc68831, "utf-8");
  } catch (_0x2c01bd) {
    return null;
  }
}
function writeCursorInfoFile(_0x29b07d) {
  try {
    const _0x74b024 = getCursorInfoPath();
    fs.writeFileSync(_0x74b024, _0x29b07d, "utf-8");
    return true;
  } catch (_0x21921d) {
    return false;
  }
}
function getCursorProConfigPath() {
  const _0x308069 = process.platform;
  const _0x1c7d55 = os.homedir();
  if (_0x308069 === "darwin" && _globalAppRoot) {
    let _0x5d9394 = path.join(_globalAppRoot, "resources", "app");
    if (!fs.existsSync(_0x5d9394)) {
      _0x5d9394 = _globalAppRoot;
    }
    const _0x84932b = path.join(_0x5d9394, "extensions", "cursor-always-local", "dist");
    const _0x13791d = path.join(_0x84932b, "config.json");
    console.log("[pathUtils] macOS配置文件路径:", _0x13791d);
    return _0x13791d;
  } else if (_0x308069 === "win32") {
    const _0x302b96 = path.join(_0x1c7d55, "AppData", "Roaming", "CursorPro");
    return path.join(_0x302b96, "config.json");
  } else {
    const _0x2656d = path.join(_0x1c7d55, ".config", "CursorPro");
    return path.join(_0x2656d, "config.json");
  }
}
function cursorProConfigExists() {
  const _0x4cd1a0 = getCursorProConfigPath();
  return fs.existsSync(_0x4cd1a0);
}
function ensureCursorProDir() {
  try {
    const _0x49cadd = getCursorProConfigPath();
    const _0x383b1c = path.dirname(_0x49cadd);
    if (!fs.existsSync(_0x383b1c)) {
      fs.mkdirSync(_0x383b1c, {
        recursive: true,
        mode: 493
      });
    }
    try {
      fs.accessSync(_0x383b1c, fs.constants.R_OK | fs.constants.W_OK);
      return true;
    } catch (_0x377c3e) {
      return false;
    }
  } catch (_0x16dc48) {
    return false;
  }
}
function readCursorProConfig() {
  try {
    const _0x37e1c1 = getCursorProConfigPath();
    if (!fs.existsSync(_0x37e1c1)) {
      return null;
    }
    try {
      fs.accessSync(_0x37e1c1, fs.constants.R_OK);
    } catch (_0x10feda) {
      return null;
    }
    const _0x2bad6b = fs.readFileSync(_0x37e1c1, "utf-8");
    const _0x2a107e = JSON.parse(_0x2bad6b);
    return _0x2a107e;
  } catch (_0x704294) {
    return null;
  }
}
function writeCursorProConfig(_0x398e79) {
  try {
    if (!ensureCursorProDir()) {
      return false;
    }
    const _0x1f45a5 = getCursorProConfigPath();
    if (fs.existsSync(_0x1f45a5)) {
      try {
        fs.accessSync(_0x1f45a5, fs.constants.W_OK);
      } catch (_0x27a7eb) {
        return false;
      }
    }
    const _0x127efc = JSON.stringify(_0x398e79, null, 4);
    fs.writeFileSync(_0x1f45a5, _0x127efc, {
      encoding: "utf-8",
      mode: 420
    });
    return true;
  } catch (_0x5f4052) {
    return false;
  }
}
function updateCursorProConfigField(_0x4d969c, _0x3c963a) {
  try {
    let _0x3a642c = readCursorProConfig() || {
      premium_usage: 0,
      max_premium_usage: 50,
      last_updated: "",
      uuid: ""
    };
    _0x3a642c[_0x4d969c] = _0x3c963a;
    _0x3a642c.last_updated = new Date().toISOString();
    return writeCursorProConfig(_0x3a642c);
  } catch (_0x767069) {
    return false;
  }
}
function getCursorUpdaterPath() {
  const _0x5c701c = process.platform;
  const _0x4f8561 = os.homedir();
  switch (_0x5c701c) {
    case "win32":
      return path.join(_0x4f8561, "AppData", "Local", "cursor-updater");
    case "darwin":
      return path.join(_0x4f8561, "Library", "Application Support", "cursor-updater");
    case "linux":
      return path.join(_0x4f8561, ".local", "share", "cursor-updater");
    default:
      throw new Error("不支持的操作系统");
  }
}
function isUpdaterReadOnly(_0x172f30) {
  try {
    if (!fs.existsSync(_0x172f30)) {
      return false;
    }
    const _0x221bc7 = fs.statSync(_0x172f30);
    if (!_0x221bc7.isFile()) {
      return false;
    }
    if (process.platform === "win32") {
      try {
        fs.accessSync(_0x172f30, fs.constants.W_OK);
        return false;
      } catch (_0x2364d7) {
        return true;
      }
    } else {
      const _0x401e4b = _0x221bc7.mode;
      const _0x272afc = (_0x401e4b & 146) === 0;
      return _0x272afc;
    }
  } catch (_0x176ef3) {
    return false;
  }
}
const _0x5903c7 = {
  setAppRoot: setAppRoot,
  getAppRoot: getAppRoot,
  getKeyFiles: getKeyFiles,
  checkRequiredFiles: checkRequiredFiles,
  getCursorUserDataPath: getCursorUserDataPath,
  getCursorInstallPath: getCursorInstallPath,
  getCursorSettingsPath: getCursorSettingsPath,
  cursorSettingsExists: cursorSettingsExists,
  readCursorSettings: readCursorSettings,
  writeCursorSettings: writeCursorSettings,
  updateCursorSettingsField: updateCursorSettingsField,
  getCursorInfoPath: getCursorInfoPath,
  cursorInfoExists: cursorInfoExists,
  createCursorInfoFile: createCursorInfoFile,
  readCursorInfoFile: readCursorInfoFile,
  writeCursorInfoFile: writeCursorInfoFile,
  getCursorProConfigPath: getCursorProConfigPath,
  cursorProConfigExists: cursorProConfigExists,
  ensureCursorProDir: ensureCursorProDir,
  readCursorProConfig: readCursorProConfig,
  writeCursorProConfig: writeCursorProConfig,
  updateCursorProConfigField: updateCursorProConfigField,
  getCursorUpdaterPath: getCursorUpdaterPath,
  isUpdaterReadOnly: isUpdaterReadOnly
};
module.exports = _0x5903c7;